//
//  HUIMINGMODEL.m
//  xinhua_sx
//
//  Created by love on 14-8-17.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "HUIMINGMODEL.h"
#import "XHTableViewCell.h"
#import "XHURLString.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "customtabViewCell.h"
@interface HUIMINGMODEL()
@property (nonatomic ,strong) XHURLString *urlString;


@end

@implementation HUIMINGMODEL
@synthesize defId;
- (instancetype)init:(NSString* )stf huiming:(NSString*)huiming
{
    self = [super init];
    if (self) {
        self.urlString = [[XHURLString alloc]init];
        self.hasLoad = NO;
        self.titleArray = [NSArray array];
        self.dataArray = [NSArray array];
        [self pullData1:stf huiming:huiming];
    }
    return self;
}

-(void)pullData1:(NSString*)stf huiming:(NSString*)huiming
{
    NSLog(@"pu");
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSString* stinmg;
    if ([stf length] >0)
    {
        NSLog(@"newdid =%@",defId);
        stinmg = stf;
    }
    else if([huiming length] > 0)
    {
        stinmg = @"4";
    }
    else
    {
        stinmg = @"3";
    }
    [manager GET:[self.urlString getHuimingCOuID:stinmg] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            self.titleArray = (NSArray *)responseObject;
            //NSLog(@"%@",(NSArray*)responseObject);
            NSUserDefaults* fefe = [NSUserDefaults standardUserDefaults];
            [fefe removeObjectForKey:@"惠民栏目"];
            [fefe setObject:self.titleArray forKey:@"惠民栏目"];
            [fefe synchronize];
            for (id item in self.titleArray)
            {
                
                for (id  item1 in item)
                {
                    // NSLog(@" %@ = %@",item1,item[item1]);
                }
            }
            self.hasLoad = YES;
            [self pullData:stf huiming:huiming];
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        self.hasLoad = YES;
        NSLog(@"2222%@",error);
    }];
    
}
-(void)canall
{
    [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
}

- (void)pullData:(NSString*)stf huiming:(NSString*)huiming;
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSString* stinmg;
    if ([stf length] >0)
    {
        stinmg = stf;
    }
    else if([huiming length] > 0)
    {
        stinmg = @"4";
    }
    else
    {
        stinmg = @"3";
    }
    
    
    [manager GET:[self.urlString getHuiMingNEW:stinmg] parameters:nil success:^(AFHTTPRequestOperation*operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            
            self.dataArray = (NSArray *)responseObject;
           // NSLog(@"%@",responseObject);
            NSUserDefaults* fefe = [NSUserDefaults standardUserDefaults];
            [fefe removeObjectForKey:@"惠民新闻"];
            [fefe setObject:self.dataArray forKey:@"惠民新闻"];
            [fefe synchronize];

            self.hasLoad = YES;
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"%@",error);
        self.hasLoad = YES;
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray count];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string  = @"onepeace";
    /*XHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
     if (!cell) {
     NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"XHTableViewCell" owner:self options:nil];
     cell= [array objectAtIndex:0];
     }
     if ([self.dataArray count] >0) {
     cell.title.text= [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"title"];
     cell.describe.text= [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"description"];
     NSString *imageString = [NSString stringWithFormat:@"%@%@",self.urlString.ipAndPort,[[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"image"]];
     
     [cell.image setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic"]];
     id hits = [[self.dataArray objectAtIndex:indexPath.section] objectForKey:@"hits"];
     if ( hits) {
     cell.hits.text = [NSString stringWithFormat:@"%@",hits];
     }
     
     }
     return cell;*/
    customtabViewCell* cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell)
    {
        cell = [[customtabViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    if ([self.dataArray count] > 0)
    {
         [cell setdic:[self.dataArray objectAtIndex:indexPath.row]];
    }
    
    [cell setLan:[[self.titleArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
    
}


@end
